package ar.fiuba.fallar2.pac_man.fantasma.persist;

import java.awt.Point;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ar.fiuba.fallar2.pac_man.AbstractDataMapper;
import ar.fiuba.fallar2.pac_man.AbstractPersonaje;
import ar.fiuba.fallar2.pac_man.DBManager;
import ar.fiuba.fallar2.pac_man.IBusinessObject;
import ar.fiuba.fallar2.pac_man.fantasma.model.Fantasma;

public class FantasmaDataMapper extends AbstractDataMapper{

	@Override
	public List findAll() 
	{
		try {
			List<IBusinessObject> lista = new ArrayList<IBusinessObject>();
			String sqlQuery = "SELECT * FROM "+getTable();
			ResultSet r = DBManager.getInstance().find(sqlQuery).getResultSet();
			while (r.next()) 
				lista.add(createBO(r));
			
			r.getStatement().close();
			return lista;
		 } catch (SQLException e) {
			 e.printStackTrace();
			throw new RuntimeException(e);
		}
	
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public IBusinessObject load(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	

@Override
protected IBusinessObject createBO(ResultSet r) throws  SQLException {
	 Point poi=new Point();
	 int direccion=AbstractPersonaje.ABAJO;
	 Fantasma fantasma= new Fantasma(r.getInt("FT_id"),
			 				r.getInt("FT_Velocidad"),
			 			    r.getInt("FT_Color"),poi,direccion);
			 				  		  	
	return  fantasma;
}

@Override
protected String getIdColumn() {
	// TODO Auto-generated method stub
	return "FT_id";
}

@Override
protected String getTable() {
	// TODO Auto-generated method stub
	return "Fantasma";
}

@Override
protected String mapBOInsert(IBusinessObject r) throws SQLException {
	Fantasma fantasma = (Fantasma) r;
	StringBuffer sb = new StringBuffer();
	sb.append("INSERT INTO " + getTable());
	sb.append("( FT_Velocidad ,FT_Color ) VALUES (");
	sb.append(fantasma.getVelocidad()+", '");
	sb.append(fantasma.getColor()+"') ");
	return sb.toString();
}

@Override
protected String mapBOUpdate(IBusinessObject r) throws SQLException {

	Fantasma fantasma   = (Fantasma) r;
	StringBuffer sb = new StringBuffer();
	sb.append("UPDATE " + getTable());
	sb.append("  FT_Velocidad='"+fantasma.getVelocidad());
	sb.append(", FT_Color='"+fantasma.getColor());
	sb.append("' WHERE FT_Id="+fantasma.getId()+")");
	return sb.toString();
}
	

}
